{include file="public/_meta" /}
<body>
<div class="wrapper">

    {include file="public/_header" /}

    <!-- Sidebar -->
    {include file="public/_menu" /}
    <!-- End Sidebar -->

    <div class="main-panel">

        <div class="content">
            <div class="page-inner">

                <!-- 面包屑 -->
                {include file="public/_page_header" /}

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="d-flex align-items-center">
                                    <h4 class="card-title">方案列表</h4>
                                    <button class="btn btn-primary btn-round ml-auto" data-toggle="modal" data-target="#addRowModal" onClick="add_item(0)">
                                        <i class="fa fa-plus"></i>
                                        添加
                                    </button>
                                </div>

                                <form class="row">

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <select class="form-control" name="status">
                                                <option value="0">全部状态</option>
                                                <option value="1" {if condition="1 eq $status"}selected{/if}>启用</option>
                                                <option value="2" {if condition="2 eq $status"}selected{/if}>禁用</option>
                                            </select>
                                        </div>
                                    </div>


                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="plan_name" value="{$plan_name}" placeholder="请输入方案名称">
                                        </div>
                                    </div>



                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <button type="submit" class="btn btn-primary">查询</button>
                                            <a href="{:url('plan/index')}" class="btn btn-info" style="color: white;position: absolute;left: 110px;">重置</a>
                                        </div>
                                    </div>

                                </form>

                            </div>


                            <div class="card-body">
                                <!-- Modal -->
                                <div class="modal fade" id="addRowModal" tabindex="-1" role="dialog" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content" style="width: 800px;">
                                            <div class="modal-header no-bd">
                                                <h5 class="modal-title">
                                                    <span class="fw-mediumbold">添加</span>

                                                </h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <form id="form-modal">
                                                    <div class="row">

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="channel_id" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">媒体名称</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <select class="form-control input-full" id="channel_id" name="channel_id">
                                                                    {volist name="channels" id="vo" key="k"}
                                                                    <option value="{$k}">{$vo}</option>
                                                                    {/volist}
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="plan_name" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">方案名称</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <input type="text" class="form-control input-full" id="plan_name" name="plan_name" placeholder="方案名称">
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">线索配置文档地址</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <a href="" id="docx_url" target="_blank"></a>
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">线索接收地址</font></label>

                                                            <div class="col-md-8 p-0" id="clue_token" style="display: none">
                                                                <b id="clue_url"></b>
                                                            </div>
                                                            <div class="col-md-8 p-0" style="display: none">
                                                                <b id="clue_url_tmp"></b>
                                                            </div>
                                                            <div class="col-md-8 p-0">
                                                                <b class="btn btn-success btn-sm" id="find_curl" onclick="find_clue_token()">查看接收地址</b>
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="" class="col-md-3 col-form-label"><font style="vertical-align: inherit;"></font></label>

                                                            <div class="col-md-8 p-0">
                                                                <b class="btn btn-info" id="clue_url_a" data-url="" style="display: none">复制并跳转</b>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer no-bd">
                                                        <input type="hidden" name="id" value="">
                                                        <input type="hidden" id="access_token" name="access_token" value="">
                                                        <input type="hidden" id="token_url" name="token_url" value="">
                                                        <b style="display: none" id="other_url"></b>
                                                        <button type="submit" id="addRowButton" class="btn btn-primary">保存</button>
                                                        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                                    </div>
                                                </form>
                                            </div>

                                        </div>
                                    </div>
                                </div>

                                <div class="table-responsive">
                                    <table id="add-row" class="display table table-striped table-hover" >
                                        <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>方案名称</th>
                                            <th>来源媒体</th>
<!--                                            <th>方案Token</th>-->
                                            <th>推送地址</th>
                                            <th>创建时间</th>
                                            <th>修改时间</th>
                                            <th>是否启用</th>
                                            <th style="width: 10%">操作</th>
                                        </tr>
                                        </thead>

                                        <tbody>

                                        {volist name="infos" id="vo"}
                                        <tr>
                                            <td>{$vo.id}</td>
                                            <td>{$vo.plan_name}</td>
                                            <td>{$vo.channel_id | getChannelNameByUid}</td>
<!--                                            <td>{$vo.token}</td>-->
                                            <td>{$vo.url}</td>
                                            <td>{$vo.create_time}</td>
                                            <td>{$vo.update_time}</td>
                                            <td>{$vo.status|getStatus}</td>
                                            <td>
                                                <div class="form-button-action">


                                                    {if condition="$vo.status eq 1"}

                                                    <button type="button" data-toggle="tooltip" title="" onClick="admin_stop(this,'{$vo.id}')" class="btn btn-link btn-warning" data-original-title="停用">
                                                        <i class="fa fa-toggle-on"></i>停用
                                                    </button>

                                                    {else/}

                                                    <button type="button" data-toggle="tooltip" title="" onClick="admin_start(this,'{$vo.id}')" class="btn btn-link btn-success" data-original-title="启用">
                                                        <i class="fa fa-toggle-off"></i>启用
                                                    </button>
                                                    {/if}

<!--                                                    <button type="button" title="编辑" class="btn btn-link btn-primary btn-lg" data-toggle="modal" data-target="#addRowModal" onClick="edit_item('{$vo.id}')">-->
<!--                                                        <i class="fa fa-edit"></i>编辑-->
<!--                                                    </button>-->

                                                    <button type="button" data-toggle="tooltip" onClick="admin_del(this,'{$vo.id}')" title="" class="btn btn-link btn-danger" data-original-title="删除">
                                                        <i class="fa fa-times"></i>删除
                                                    </button>
                                                </div>



                                            </td>
                                        </tr>
                                        {/volist}

                                        </tbody>
                                    </table>

                                    <!-- 分页 (调取通用方法)-->
                                     <div class="layui-box layui-laypage layui-laypage-molv">{:pagination($infos)}</div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>


            </div>
        </div>

        {include file="public/_footer" /}


    </div>



</div>

{include file="public/_js" /}

</body>

<script src="__STATIC__/manage/lib/js/clipboard.min.js"></script>
<script>

    var clipboard = new ClipboardJS('#clue_url_a', {
        container: document.getElementById('addRowModal'),
        text: function () {
            var clue_url = $("#clue_url").html();

            if(clue_url == ''){
                layer.msg('请先查看线索接收地址',{icon:2,time:2000});return false;
            }

            var c = document.createElement('div');
            c.innerHTML = clue_url;
            clue_url = c.innerText || c.textContent;
            c = null;

            return clue_url;
        },
    });

    clipboard.on('success', function (e) {
        layer.msg('复制成功！', {
            icon: 1,
            time: 2000
        }, function(){
            var other_url = $("#other_url").html();

            window.open(other_url)
        });
    });

    clipboard.on('error', function (e) {
        console.log('复制失败！');
    });

</script>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
    var SCOPE = {
        'status_url':"{:url('plan/status')}",
        'delete_url':"{:url('plan/delete')}",
        'item_url':"{:url('plan/edit')}",
        'add_url':"{:url('plan/save')}",
        'clue_url':"{:url('plan/clue_url')}",
        'get_access_token':"{:url('plan/get_access_token')}",
    };

    $("#channel_id").change(function (){
        if($(this).val() == 0){
            $("#clue_url_a").hide();
        }

        get_clue_url($(this).val())

    })

    function get_clue_url(id)
    {
        $.ajax({
            type: 'GET',
            url: SCOPE.clue_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $("#clue_url_tmp").html(res.data.clue_url);
                    $("#docx_url").html(res.data.docx_url);
                    $("#docx_url").attr('href',res.data.docx_url);
                    if(res.data.other_url == ''){
                        $("#clue_url_a").hide();
                    }else{
                        $("#clue_url_a").show();
                    }

                    $("#other_url").html(res.data.other_url);
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }

    function find_clue_token()
    {
        var clue_url = $("#clue_url_tmp").html();
        var plan_name = $("#plan_name").val();

        if(plan_name == ''){
            layer.msg('方案名称不能为空',{icon:2,time:2000});return false;
        }

        $.ajax({
            type: 'GET',
            url: SCOPE.get_access_token,
            dataType: 'json',
            data: {plan_name:plan_name},
            success: function(res){
                if(res.code == 1001){
                    $("#access_token").val(res.data.access_token);
                    $("#clue_token").show();
                    $("#find_curl").hide();
                    $("#clue_url").html(clue_url+'&access_token='+res.data.access_token);
                    $("#token_url").val(clue_url+'&access_token='+res.data.access_token);
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }


    $(function(){

        get_clue_url(1)

        var commitStatus = false;
        /*表单验证*/
        $("#form-modal").validate({
            rules:{
                plan_name:{
                    required:true,
                    minlength:2,
                    maxlength:30
                }

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_url;
                if(commitStatus==true){
                    return false;
                }
                var channel_id = $("#channel_id").val();
                if(channel_id == 0){
                    layer.msg('请选择媒体',{icon:2,time:2000});return false;
                }
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

    });

    function add_item(id) {
        $("input[name='id']").val(id);
        document.getElementById('form-modal').reset();
        $("#clue_url").html('');
        $("#token_url").val('');
        $("#clue_token").hide();
        $("#find_curl").show();
        $(".fw-mediumbold").text('添加');
    }

    function edit_item(id) {
        $.ajax({
            type: 'GET',
            url: SCOPE.item_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $(".fw-mediumbold").text('编辑');
                    $("input[name='id']").val(id);
                    $("#channel_id").val(res.data.channel_id);
                    $("#plan_name").val(res.data.plan_name);
                    $("#docx_url").html(res.data.docx_url);
                    get_clue_url(res.data.channel_id);
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }



    /*删除*/
    function admin_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                type: 'POST',
                url: SCOPE.delete_url,
                dataType: 'json',
                data: {id:id},
                success: function(data){
                    if(data.code == 1001){
                        $(obj).parents("tr").remove();
                        layer.msg('删除方案成功!',{icon:1,time:2000});
                    }else{
                        layer.msg(data.msg,{icon:2,time:2000});
                    }

                },
                error:function(data) {
                    layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
                }
            });
        });
    }

    /*机构-停用*/
    function admin_stop(obj,id){
        layer.confirm('确认要停用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……
            var url = SCOPE.status_url;
            $.post(url,{status:2,id:id},function(data){
                if(data.code == 1001){
                    layer.msg('已停用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }

            });


        });
    }

    /*机构-启用*/
    function admin_start(obj,id){
        layer.confirm('确认要启用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……

            var url = SCOPE.status_url;
            $.post(url,{status:1,id:id},function(data){
                if(data.code == 1001){

                    layer.msg('已启用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }
            });
        });
    }

</script>
</html>




























